package com.topvision.myviewtest.algorithm.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.topvision.myviewtest.R;
import com.topvision.myviewtest.algorithm.util.SortTestHelper;
import com.topvision.myviewtest.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SelectActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.ll)
    LinearLayout mLinearLayout;

    private List<Button> mButtons;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_sort);
        ButterKnife.bind(this);
        addButtons();
    }

    private void addButtons() {
        mButtons = new ArrayList<>();
        SparseArray<String> sparseArray = SortTestHelper.getmSortNameArray();
        int dp10 = DisplayUtil.dip2px(this, 10);
        int sp20 = DisplayUtil.sp2px(this, 10);
        for (int i = 0; i < sparseArray.size(); i++) {
            String valueAt = sparseArray.valueAt(i);
            Button button = new Button(this);
            button.setText(valueAt);
            button.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
            button.setTextSize(sp20);
            button.setTextColor(Color.parseColor("#ffffff"));
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(dp10, dp10, dp10, dp10);
            button.setLayoutParams(layoutParams);
            button.setOnClickListener(this);
            mButtons.add(button);
            mLinearLayout.addView(button);
        }
    }

    /*@OnClick({R.id.sort_select, R.id.sort_insert})
    public void onViewClicked(View view) {
        Intent intent = new Intent(this, SortActivity.class);
        switch (view.getId()) {
            case R.id.sort_select:
                intent.putExtra(SortTestHelper.SORT_NAME_KEY, SortTestHelper.SORT_SELECT);
                break;
            case R.id.sort_insert:
                intent.putExtra(SortTestHelper.SORT_NAME_KEY, SortTestHelper.SORT_INSERT);
                break;
        }
        startActivity(intent);
    }*/

    @Override
    public void onClick(View v) {
        Intent intent = getCurrent(v);
        startActivity(intent);
    }

    private Intent getCurrent(View v) {
        Intent intent = new Intent(this, SortActivity.class);
        for (int i = 0; i < mButtons.size(); i++) {
            Button button = mButtons.get(i);
            if (button == v) {
                intent.putExtra(SortTestHelper.SORT_NAME_KEY, i);
                intent.putExtra(SortTestHelper.SORT_CLASS_KEY, SortTestHelper.getSortClassValue(i));
                return intent;
            }
        }
        return null;
    }
}
